use crate::{
    cli::{Cli, Commands},
    conf::repo_conf::RepoListFilter,
    default::CMDLINE_REPO_NAME,
    i18n::repo_list::{
        REPOLIST_REPO_ID, REPOLIST_REPO_NAME, REPOLIST_REPO_STATUS, REPOLIST_REPO_STATUS_DISABLED,
        REPOLIST_REPO_STATUS_ENABLED,
    },
    Rdnf,
};
use anyhow::bail;
use anyhow::Result;
use console::style;
impl Cli {
    pub fn get_repolist_filter(&self) -> Result<RepoListFilter> {
        match self.command {
            Commands::Repolist(ref opt) => {
                let sum = opt.all as usize + opt.enabled as usize + opt.disabled as usize;
                if sum > 1 {
                    bail!("you can only choose one of three options (all,enabled,disabled)")
                };
                let filter = match opt.all {
                    true => RepoListFilter::All,
                    false => match opt.enabled {
                        true => RepoListFilter::Enabled,
                        false => match opt.disabled {
                            true => RepoListFilter::Disabled,
                            false => RepoListFilter::Enabled,
                        },
                    },
                };
                return Ok(filter);
            }
            _ => Ok(RepoListFilter::Enabled),
        }
    }
}

impl Rdnf {
    pub fn repo_list(&self) -> Result<()> {
        let filter = self.cli.get_repolist_filter()?;
        let repos = self
            .repo_confs
            .iter()
            .filter(|repo| {
                // let repo_lock=repo.lock();
                if repo.name.as_str() == CMDLINE_REPO_NAME {
                    false
                } else {
                    match filter {
                        RepoListFilter::All => true,
                        RepoListFilter::Enabled => repo.base.enabled,
                        RepoListFilter::Disabled => !repo.base.enabled,
                    }
                }
            })
            .collect::<Vec<_>>();
        if repos.len() > 0 {
            let max_name_size = repos
                .iter()
                .max_by(|x, y| x.name.len().cmp(&y.name.len()))
                .unwrap()
                .name
                .len();
            let max_full_size = repos
                .iter()
                .max_by(|x, y| x.full_name.len().cmp(&y.full_name.len()))
                .unwrap()
                .full_name
                .len();
            let title = format!(
                "{:<left$}\t\t{:<center$}\t{}",
                REPOLIST_REPO_ID,
                REPOLIST_REPO_NAME,
                REPOLIST_REPO_STATUS,
                left = max_name_size,
                center = max_full_size,
            );
            self.term.write_line(title.as_str())?;
            for repo in repos {
                let status = match repo.base.enabled {
                    true => {
                        format!("{}", style(REPOLIST_REPO_STATUS_ENABLED).green())
                    }
                    false => {
                        format!("{}", style(REPOLIST_REPO_STATUS_DISABLED).red())
                    }
                };
                let item = format!(
                    "{:<left$}\t\t{:<center$}\t{}",
                    repo.name.as_str(),
                    repo.full_name.as_str(),
                    status.as_str(),
                    left = max_name_size,
                    center = max_full_size,
                );
                self.term.write_line(item.as_str())?;
            }
        }
        Ok(())
    }
}
#[test]
fn test() {
    let v = vec![
        ("fedora", "Fedora 37 - x86_64"),
        (
            "fedora-cisco-openh264",
            " Fedora 37 openh264 (From Cisco) - x86_64",
        ),
        ("fedora-modular", "Fedora Modular 37 - x86_64 "),
    ];
    let p = v
        .iter()
        .max_by(|x, y| x.0.len().cmp(&y.0.len()))
        .unwrap()
        .0
        .len();
    dbg!(p);
    dbg!(v);
    dbg!("fedora-cisco-openh264".len());
}
